import request from "@/axios/request";
import { defineStore } from "pinia";
import { store } from "./store";
import { useAppUserStore } from "./appUser";
import { computed } from "vue";

const appUserStore = useAppUserStore(store);
export const useOrganisationUserStore = defineStore("organisationUser", () => {

  const token = computed(() => appUserStore.token);

  const currentUser = computed(() => appUserStore.currentUser);

  const thirdpartToken = computed(() => appUserStore.thirdpartToken);

  function login(userInfo) {
    const { username, password, appid } = userInfo;
    return new Promise((resolve, reject) => {
      return request
        .post({
          url: "/api/Account/Login?appid="+appid,
          method: "post",
          data: { uid: username, pwd: password, appId: appid },
        })
        .then((res) => {
          appUserStore.setToken(res.token);
          appUserStore.setCurrentUser(res.userInfo,true);
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }
  function autoLogin(userInfo) {
    const { username, password } = userInfo;
    return new Promise((resolve, reject) => {
      return request
        .post({
          url: "/api/Account/autoLogin",
          method: "post",
          data: { uid: username, pwd: password },
          headers: {
            "thirdpartauthorization": userInfo.access_token,
            "appid": userInfo.appid,
            "access_token": userInfo.access_token,
            "pluginid":userInfo.pluginId
          }
        })
        .then((res) => {
          appUserStore.setToken(res.token);
          appUserStore.setCurrentUser(res.userInfo,true);
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }
  function logout() {
    return new Promise((resolve) => {
      return request
        .post({
          url: "/api/Account/Logout",
          method: "post",
        })
        .then(() => {
          appUserStore.removeToken(); // must remove  token  first
          resolve();
        })
        .catch(() => {
          appUserStore.removeToken(); // must remove  token  first
          resolve();
        });
    });
  }
  function getInfo() {
    return new Promise((resolve, reject) => {
      return request
        .get({
          url: "/api/Account/GetUserInfo",
          method: "get",
          data: {},
        })
        .then((res) => {
          appUserStore.setCurrentUser(res);
          resolve();
          return res;
        })
        .catch((error) => {
          reject(error);
        });
    });
  }
  function getInfo() {
    return new Promise((resolve, reject) => {
      return request
        .get({
          url: "/api/Account/GetUserInfo",
          method: "get",
          data: {},
        })
        .then((res) => {
          appUserStore.setCurrentUser(res);
          resolve();
          return res;
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  function getAllList(params) {
    return request
      .get({
        url: "/api/Account/GetAllList",
        method: "get",
        params: params,
      })
      .then((res) => {
        if (!appUserStore.token && res) {
          appUserStore.setCurrentUser(res[0]);
        }

        return res;
      });
  }

  function getAllAppIds(params) {
    return request
      .get({
        url: "/api/Account/GetAllAppIds",
        method: "get",
        params: params,
      })
      .then((res) => {
        return res;
      });
  }

  return {
    token,
    currentUser,
    thirdpartToken,
    login,
    autoLogin,
    logout,
    getInfo,
    getAllList,
    getAllAppIds,
  };
});

export const useOrganisationUserStoreWithOut = () => {
  return useOrganisationUserStore(store);
};
